//
//  LS_SoreItemTableViewcell.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/10.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_SoreItemTableViewcell.h"
#import "LS_CollectionViewHorizontaLayout.h"
#import "LS_mainpageSecond1CollectionViewcell.h"
#import "NSArray+LSArray.h"
#import "LS_SoreItemModel.h"
#import "LS_FindViewModel.h"
@interface LS_SoreItemTableViewcell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *ls_collectionView;
@property(nonatomic,strong)NSMutableArray *lwc_array;
@property(nonatomic,strong)LS_FindViewModel  *ls_model;//主 model
@end
@implementation LS_SoreItemTableViewcell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
 
        [self.soreItemSignal subscribeNext:^(id x) {
            NSLog(@"x--------%@",x);
        }];
    }return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
}
#pragma mark -
#pragma mark --绑定View的数据
- (void)bindViewModel:(id)viewModel withParams:(NSDictionary *)params
{
    NSLog(@"viewModel----%@------params-----%@",viewModel,params);
    LS_FindViewModel *model = viewModel;
    self.ls_model = model;
     self.lwc_array = [NSArray ls_splitArray:model.soreItemArr WithSubSize:8].mutableCopy;
    
    [self.ls_collectionView reloadData];
}
-(UICollectionView *)ls_collectionView
{
    return LS_LAZY(_ls_collectionView, ({
        LS_CollectionViewHorizontaLayout *flowlayout = [[LS_CollectionViewHorizontaLayout alloc]init];
        flowlayout.minimumLineSpacing=0;
        flowlayout.minimumInteritemSpacing=0;
        flowlayout.itemCountPerRow = 4;
        flowlayout.rowCount = 2;
        flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        CGFloat itemWinth = ScreenWidth/4;
        CGFloat itemHeight = ScreenWidth/4*1.2;
        flowlayout.itemSize =CGSizeMake(itemWinth, itemHeight);
        
        UICollectionView *myCollectionView  = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, itemHeight*2) collectionViewLayout:flowlayout];
        myCollectionView.delegate=self;
        myCollectionView.dataSource=self;
        [self addSubview:myCollectionView];
        myCollectionView.backgroundColor = [UIColor clearColor];
        myCollectionView.pagingEnabled = YES;
        [myCollectionView registerClass:[LS_mainpageSecond1CollectionViewcell class] forCellWithReuseIdentifier:@"cell"];
        myCollectionView;
    }));
}
-(NSMutableArray *)lwc_array
{
    return LS_LAZY(_lwc_array, @[].mutableCopy);
}
#pragma maek-----collectionview delete
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *arr =self.lwc_array[section];
    
    return arr.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.lwc_array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LS_mainpageSecond1CollectionViewcell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *arr = self.lwc_array[indexPath.section];
    LS_SoreItemModel *model = arr[indexPath.row];
    cell.model = model;
    return cell;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = self.lwc_array[indexPath.section];
   LS_SoreItemModel *model = arr[indexPath.row];
    
//    [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [self.ls_model.didSelectsSoreItemCommand execute:model];
//        [subscriber sendNext:nil];
//        [subscriber sendCompleted];
//        return nil;
//    }];
}


@end
